package com.kinyx.framework.crawler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.util.XArrayList;

import org.junit.Test;

import com.kinyx.framework.bean.GetArgs;
import com.kinyx.framework.bean.KCrawlerInfo;
import com.kinyx.framework.bean.define.ICrawlerInfo;
import com.kinyx.framework.file.KPathUtils;
import com.kinyx.framework.utils.KDateTimeUtils;

public class KCrawler_Tester_1 {

	String startDate = KDateTimeUtils.toString(new Date().getTime() - (24 * 60 * 60 * 1000), "yyyyMMdd");
	List<String> stockCodes = new XArrayList<String>().xAdd("000001").xAdd("000002").xAdd("000003");

	String URL = "http://quotes.money.163.com/service/chddata.html";
	String CHARSET = "gb2312";
	String filePath = KPathUtils.target() + "/chddata";

	public void test(final int maxThreads) {
		final List<ICrawlerInfo> infos = new ArrayList<>(this.stockCodes.size());
		this.stockCodes.forEach(stockCode -> {
			infos.add(KCrawlerInfo.builder()//
					.requestArgs(//
							new GetArgs(this.URL, this.CHARSET)//
									.userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")//
									.param("code", "1" + stockCode)//
									.param("start", this.startDate)//
			)//
					.filePath(this.filePath + "/" + stockCode)//
					.fileCharset(this.CHARSET)//
					.build());
		});
		KCrawler.crawl(null, infos, maxThreads);
	}

	@Test
	public void test1() { this.test(0); }

	@Test
	public void test2() { this.test(2); }

}
